<?php

class Application_Model_Backlog extends Application_Model_DomainObject {
    
	protected $_id;
        
        protected $_dateCreated;
        
	protected $_content;

	protected $_comment;

	protected $_sourceType;
        
        protected $_sourceId;
        
        protected $_solveDay;

	protected $_solveName;
        
        protected $_status;
        
        protected $_operator;


        /**
	 * 未处理
	 */
	const STATUS_default = 0;
        /**
         * 处理中
         */
        const STATUS_dispose = 1;
	/**
	 * 已处理
	 */
	const STATUS_processed = 2;
        /**
         * 客户
         */
        const Source_Service = 1;
        /**
         * 分配任务
         */
        const Source_Assign = 2;
        /**
         * 跨部门协助
         */
        const Source_Assist = 3;
        
        public static function getStatusAll()
        {
            return array(
                       self::STATUS_default => "待处理"
                    ,  self::STATUS_dispose => "处理中"
                    ,  self::STATUS_processed => "已处理"
            );
        }

        public function getStatusName()
        {
            $arrStatus = self::getStatusAll();
            return $arrStatus[$this->_status];
        }
        
        public static function getSourceAll()
        {
            return array(
                       self::Source_Service => "客户提供"
                    ,  self::Source_Assign => "部门分配"
                    ,  self::Source_Assist => "跨部门协助"
            );
        }
        public static function getSourceTypeAll()
        {
            return array(
                       self::Source_Assign => "部门分配"
                    ,  self::Source_Assist => "跨部门协助"
            );
        }
        public function getSourceName()
        {
            $arrSource = self::getSourceAll();
            return $arrSource[$this->_sourceType];
        }

        public static function getBacklogFilter($backlogSearch,$pagesize,$pageindex)
        {
            
            return Application_Model_Backlog::getDao()->queryPage($backlogSearch->getSearchFileds(), ' id desc', $pagesize, $pageindex);;
        }
        
        public function getSourceUrl()
        {
            if($this->_sourceId != "" && $this->_sourceId != null){
                switch ($this->_sourceType) {
                    case self::Source_Service:
                        return "<a href='/admini/manageadmin/servicedetail/id/".$this->_sourceId."' target='_blank' class='indexa'>客服记录</a>";
                        break;
                    default:
                        return ;
                        break;
                }
            }
            return ;
        }

        public function getSolveUserName()
        {
            $solveUserItem = Application_Model_adminuser::load($this->_solveName);
            return $solveUserItem->nikename;
        }

        public function getKnowUserAllName()
        {
            $knowUserAllItem = Application_Model_KnowUser::getAllByBacklogIdType($this->_id,  Application_Model_KnowUser::Type_Service);
            $nikeName = "";
            foreach ($knowUserAllItem as $knowUserVal) {
                $userItem = Application_Model_adminuser::load($knowUserVal->UserId);
                if($userItem != null){
                    $nikeName .= $userItem->nikename."，";
                }
            }
            return $nikeName;
        }

        public static function addBacklog($content,$solveName,$solveDay,$comment,$sourceType,$sourceId)
        {
            
            $backlogItem = new Application_Model_Backlog();
            $backlogItem->Content = $content;
            $backlogItem->Comment = $comment;
            $backlogItem->SolveName = $solveName;
            $backlogItem->SolveDay = $solveDay;
            $backlogItem->SourceType = $sourceType;
            $backlogItem->SourceId = $sourceId;
            $backlogItem->DateCreated = date("Y-m-d H:i:s");
            $backlogItem->Status = self::STATUS_default;
            $backlogItem->Operator = Uuch_Util::getUserNikeName();
            $backlogItem->save();
            return $backlogItem->Id;
        }
        
        public static function getCountBySolve($userId)
        {
            return self::getDao()->count(array("SolveName = "=>$userId," and status != "=>  self::STATUS_processed));
        }
}